<?php

/*
	Class: Config

	About: Author
		Jaybill McCarthy

	About: License
		<http://communit.as/docs/license>

	About: See Also
	 	<Cts_Db_Table_Abstract>
	 	Zend_Db_Table
*/
class Config extends Cts_Db_Table_Abstract {

	/* Group: Protected Class Variables */

	/*
		Variable: $_name
			name of the table being used
	*/
    protected $_name = 'default_config';

    /*
		Variable: $_primary
			primary key of the table being used
	*/
    protected $_primary = array('module', 'ckey');

	/* Group: Instance Methods */

	/*
		Function: keyExists
			Verifies the existence of a key in the configuration for a particular module.

		Arguments:
			module - The ID of the module to search. (the haystack)
			ckey - The key to search for. (the needle)

		Returns: boolean
	*/
	function keyExists($module = "default", $ckey) {
		$where = $this->getAdapter()->quoteInto("ckey = ?", $ckey);
		$where .= $this->getAdapter()->quoteInto(" and module = ?", $module);
		if ($this->getCountByWhereClause($where) == 0) {
			return false;
		} else {
			return true;
		}
	}

	/*
		Function: get

		Arguments:
			module - TBD
			ckey - TBD

		Returns: TBD
	*/
	function get($module = "default", $ckey) {
		$where = $this->getAdapter()->quoteInto("ckey = ?", $ckey);
		$where .= $this->getAdapter()->quoteInto(" and module = ?", $module);
		$value = $this->fetchRow($where);
		if (!is_null($value)) {
			return $value->value;
		} else {
			return null;
		}
	}

	/*
		Function: set

		Arguments: 
			module - TBD
			ckey - TBD
			value - TBD
			is_cached - TBD

		Returns: void
	*/
	function set($module = "default", $ckey, $value, $is_cached = false) {
		if (!$this->keyExists($module, $ckey)) {
			$data = array(
				"ckey" 		=> $ckey,
				"module"	=> $module,
				"value"		=> $value,
			);
			if ($is_cached) {
				$data['is_cached'] = 1;
			} else {
				$data['is_cached'] = 0;
			}
			parent::insert($data);
		} else {
			$where = $this->getAdapter()->quoteInto("ckey = ?", $ckey);
			$where .= $this->getAdapter()->quoteInto(" and module = ?", $module);
			$data = array("value" => $value);
			if ($is_cached) {
				$data['is_cached'] = 1;
			} else {
				$data['is_cached'] = 0;
			}
			parent::update($data, $where);
		}
	}

	/*
		Function: cache

		Arguments: none

		Returns: void
	*/
	function cache() {
		$basepath = Zend_Registry::get('basepath');
		$config_cache_file = $basepath."/etc/cached_config.ini";
		if (file_exists($config_cache_file)) {
			unlink($config_cache_file);
		}
		if (!file_exists($config_cache_file)) {
			$cacheable = $this->fetchAll('is_cached = 1');
			$config_cache = array();
			if (count($cacheable) > 0) {
				foreach ($cacheable as $config_item) {
					$config_cache[$config_item->module.".".$config_item->ckey ] = $config_item->value;
				}
			}
			$comment = "THIS FILE IS AUTO-GENERATED. DO NOT EDIT THIS FILE DIRECTLY. YOUR CHANGES WILL BE OVERWRITTEN.";
			$config_cache_string = Cts_ConfigFile::makeSection("cached", "Cached Config", $comment, $config_cache);
			file_put_contents($config_cache_file, $config_cache_string);
		}
	}
	
}
